package com.itheima.controller;

import com.itheima.common.PageResult7;
import com.itheima.common.Simple;
import com.itheima.entity.Subjects;
import com.itheima.entity.SubjectsDto;
import com.itheima.service.SubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author cfr
 * @date 2022/1/9
 * @TIME:21:09
 */
@RestController
@RequestMapping("/subjects")
public class SubjectsController {
    @Autowired
    private SubjectsService subjectsService;
    @Autowired
    private HttpServletRequest request;

    @PutMapping("/{id}")
    public String modify(@RequestBody Subjects subjects) {
        return subjectsService.modify(subjects);
    }

    @DeleteMapping("/{id}")
    public String delete(@RequestBody SubjectsDto subjectsDto) {

        return subjectsService.deleteById(subjectsDto);
    }

    @PostMapping()
    public String add(@RequestBody Subjects subjects) {
        // Long user=((User)request.getSession().getAttribute("user")).getId();
        //subjects.setCreatorID(Math.toIntExact(user));
        subjects.setCreatorID(2);
        return subjectsService.add(subjects);
    }

    @GetMapping("/simple")
    public Simple[] simple(){
        return subjectsService.simple();
    }

    @GetMapping
    public PageResult7 fingPage(Long page, Long pagesize, String subjectName){
        return subjectsService.finfPage(page,pagesize,subjectName);
    }
    @GetMapping("/{id}")
    public SubjectsDto findById(@PathVariable Integer id){
        return subjectsService.findById(id);
    }

}
